package org.pi.common.reflect

trait ReflectApi {
  def get(ref: Object, name: String): Any = ref.getClass.getMethods.find(_.getName == name).get.invoke(ref)
  def set(ref: Object, name: String, value: Any): Unit = ref.getClass.getMethods.find(_.getName == name + "_$eq").get.invoke(ref, value.asInstanceOf[AnyRef])
  def set(ref: Object, params: Any*): Unit = {
	if (params.length%2!=0) throw new Error("only pairs of names,value are allowed")
	for (k<-0 until params.length/2) {
		val name = params(2*k+0).asInstanceOf[String]
		val value = params(2*k+1)
		set(ref, name, value)
	}
  }
}

object Api extends ReflectApi {
  def main(args: Array[String]) = {
	  println("Reflect START")
	  val l = new org.pi.common.plot.drawables.Line(0,1,2,3)
	  set(l,"lineWidth",5)
	  println("l.lineWidth=="+l.lineWidth)
	  println("Reflect END")
  }
}